﻿@model  RBAC.Web.Models.RoleModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities

@{
    var userProfile = Session["UserProfile"] as RbacLib.Models.UserProfile;
    var @privilageUpdateRole = RbacUtil.SetUpdateRoleButton(userProfile);
    var @privilageDeleteRole = RbacUtil.SetDeleteRoleButton(userProfile);

    //privilageUpdateRole = string.Empty;
    //privilageDeleteRole = string.Empty;
}

@{
    ViewBag.Title = "Edit Role";

			string returnUrl = Url.Action("ListRole", "Role");
	}
@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js?3")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />
    @*<link href="@Url.Content("~/Content/site2.css")" rel="stylesheet" type="text/css" />*@

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
							SetDatePicker("CreateDate", new Date(), '-71Y', 'c-70:c');
			            SetDatePicker("FreeDate");
            
			//How to setup Lookup
			
			//Master Detail
			 
							 
							 
							 
							 
							 
							 
				 
					SetOneToManyFor("Permission", '@Url.Action("DeletePermissions", "Permission")', '@Url.Action("EditRole", "Role", new { RoleCode = Model.RoleCode })', "form-Role");
							        });
    </script>
}

<h2>Role Detail</h2>
@using (Html.BeginForm("Update", "Role", FormMethod.Post, new { @id = "form-Role", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(false)
    <fieldset>
        <legend>RoleModel</legend>

							
		<div class="two-column-container">
			@Html.HiddenFor(model => model.Id)
      					 
			
										<div class="texteditor">
				@Html.LabelFor(model => model.RoleCode)
                @Html.TextBoxFor(model => model.RoleCode)
									@Html.HiddenFor(model => model.Id)
					<input type="hidden" name="Mode" value="Edit" />
					<input type="hidden" name="OldValue" value="@(Model.RoleCode == null ? "" : Model.RoleCode )" />
							</div>
									 
			
										<div class="texteditor">
				@Html.LabelFor(model => model.RoleName)
                @Html.TextBoxFor(model => model.RoleName)
							</div>
																 
			
										<div class="texteditor">
				@Html.LabelFor(model => model.Description)
                @Html.TextBoxFor(model => model.Description)
							</div>
									 
			
			             <div class="texteditor">
                @Html.LabelFor(model => model.CreateDate)
                @Html.DatePickerFor(model => model.CreateDate, "CreateDate",true)
                            </div>
													 
			
				
            <div class="texteditor">
                @Html.LabelFor(model => model.RoleStatus)
                @Html.RadioButtonForSelectList(model => model.RoleStatus, RoleStatusMapper.GetRoleStatusSelectList())
                            </div>
						 
			
																			</div>
		<div class="bottom-menu clear">
            <input type="button" name="btnBack" value="Cancel" id="button-back" onclick="location.href='@returnUrl'"/>
            <input type="Button" value="Save" id="button-save" onclick="JavaScript: SaveButtonClick()" @privilageUpdateRole />
            <input type="button" name="btnDelete" value="Delete" id="button-delete" onclick="JavaScript: DeleteButtonClick()" @privilageDeleteRole />
        </div>
    </fieldset>

	// Master Detail Dest Class
														
	bool highlightPermission = false;
    if (ViewData["PermissionId"] != null)
    {
        try
        {
            int index = Model.Permissions.IndexOf(Model.Permissions.FirstOrDefault(m => m.Id == Convert.ToInt32(ViewData["PermissionId"])));
            if (index > -1)
            {
                var item = Model.Permissions.ElementAt(index);
                Model.Permissions.RemoveAt(index);
                Model.Permissions.Insert(0, item);

                highlightPermission = true;
            }
        }
        catch { }
    }
	var Permissiongrid = new WebGrid(source: Model.Permissions, canPage: false);
	if (highlightPermission)
    {
        Permissiongrid.SelectedIndex = 0;
    }
    <input type="hidden" id="Permission_hid_delete" value="Delete" name="DeleteButton" /> 
    <fieldset>
        @Html.Hidden("Role_Id", Model.Id)
        <legend>Permission</legend>  
        <div class="top-menu">          
            <input id="Permission-button-add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Permission", new { Role_id = Model.Id, RoleCode = Model.RoleCode })'"/>
            <input type="button" value="Delete" id="Permission-button-delete" />
        </div>
        <div id="Permission-confirm-delete" title="Confirm delete." style="display:none">
            <span>Do you want to delete?</span>
        </div>
        <div id="Permission-dialog-alert" title="Delete Permission" style="display:none">
          <span> No Permission was selected.</span>
        </div>
        
		@Html.DisplayMessage(Convert.ToString(ViewData["PermissionMessage"]), ViewData["PermissionStatus"] as bool?)
    
    @if (Model.Permissions != null && Model.Permissions.Count > 0)
    {    
        <div style="overflow:scroll; width: 750px; height:auto; max-height: 400px;">
		@Permissiongrid.GetHtml( 
        htmlAttributes: new { id = "Permission"},
        tableStyle: "tableMain Permissiongrid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
		selectedRowStyle: "selected-row",
        columns: Permissiongrid.Columns(
            Permissiongrid.Column(header: "", style: "width_0per", format: @<text><input name="PermissionCheckbox" type="checkbox" value="@item.Id" /></text>),
            Permissiongrid.Column(header: "#",  style: "width_0per",format: item => item.WebGrid.Rows.IndexOf(item) + 1)
            //TODO : สามารถ Edit ได้ --
            // ทดสอบ Edit  
        					
			
			
												
							
						@* ถ้า attribute ของลูก เป็น LinkEdit *@
			,Permissiongrid.Column(header: "PermissionName", style: "width_0per", columnName: "PermissionName",
            format: item =>
            {
				string link = "<a href=\"{0}?PermissionName={2}\" class=\"word_wrap\">{1}</a>";
                return Html.Raw(string.Format(link, @Url.Content("~/Permission/EditPermission"), item.Value.PermissionName, item.Value.PermissionName));
			})
			
			
												
							
			
			
			           @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น String *@
		   , Permissiongrid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
                format: item =>
                {
                    string Description = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description) ? "" : item.Value.Description));
                })   
			

			
						
							
			
			
			            @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น DateTime *@
			,Permissiongrid.Column(header: "Create Date", style: "width_0per", columnName: "Create Date",
                format: item =>
                {
                    string CreateDate = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(CreateDate, DateTime.Parse(item.Value.CreateDate.ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"))));
                }
            )
			

			
						
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
			, Permissiongrid.Column(header: "RoleRole Code", canSort: false, style: "width_100per", columnName: "RoleRole Code",
                format: item =>
                {
                    string RoleCode = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(RoleCode, String.IsNullOrWhiteSpace(item.RoleRoleCode) ? "" : item.Value.RoleRoleCode));
                })  

						@* END ดึง External Lookup Field *@
			 
			

						
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
			, Permissiongrid.Column(header: "FunctionFunc Name", canSort: false, style: "width_100per", columnName: "FunctionFunc Name",
                format: item =>
                {
                    string FuncName = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(FuncName, String.IsNullOrWhiteSpace(item.FunctionFuncName) ? "" : item.Value.FunctionFuncName));
                })  

						@* END ดึง External Lookup Field *@
			 
			

						
							
			
			
												
							
            //chayawooth.puy
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
            //, Permissiongrid.Column(header: "RoleLookupRole Code", canSort: false, style: "width_100per", columnName: "RoleLookupRole Code",
            //    format: item =>
            //    {
            //        string RoleCode = "<div class=\"word_wrap\">{0}</div>";
            //        return Html.Raw(string.Format(RoleCode, String.IsNullOrWhiteSpace(item.RoleLookupRoleCode) ? "" : item.Value.RoleLookupRoleCode));
            //    })  

						@* END ดึง External Lookup Field *@
			 
			

						
		             
        ))
		</div>
	}
    else
    {
            <div class="emptyRow">No Permission for this Role.</div>
    }

	</fieldset>

		}

<div id="confirm-delete" title="Confirm delete." style="display: none">
    <span>Do you want to delete?</span>
</div>

<div id="confirm-save" title="Confirm save." style="display: none">
    <span>Do you want to save?</span>
</div>

<script type="text/javascript">
    function DeleteButtonClick() {
        $("#confirm-delete").dialog({
            modal:true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $.post('@Url.Action("DeleteRoleById", "Role")',$("#form-Role").serialize(),function (data) {
                        var s = '@returnUrl'
                        if(s.indexOf("?") !== -1)
                            window.location.href = s + '&result=' + data + '&operation=delete';   
                        else
                            window.location.href = s + '?result=' + data + '&operation=delete'; 
                    });
                },
                No:function(){
                    $(this).dialog("close");
                }
            }
        })
    }

    function SaveButtonClick() {
        $("#confirm-save").dialog({
            modal: true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $("#form-Role").submit();
                },
                No: function () {
                    $(this).dialog("close");
                }
            }
        })
    }
</script>